package com.gxks.lhs.controller.admin;


import com.gxks.lhs.constants.R;
import com.gxks.lhs.entity.Ad;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 主页轮播图 前端控制器
 * </p>
 *
 * @author LuoHaisong
 * @since 2024-03-29
 */
@Api(tags = "主页轮播图广告")
@Slf4j
@RestController
@RequestMapping("/ad")
public class AdController {

    @Autowired
    private com.gxks.lhs.service.IAdService adService;

    @ApiOperation("获取主页轮播图")
    @GetMapping("")
    public R getIndexImgs(@RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                          @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        return R.success(adService.queryAllAdForAdmin(current, size));
    }

    @ApiOperation("添加轮播图")
    @PostMapping("/save")
    public R addAd(@RequestBody Ad ad) {
        ad.setStatus(true);
        if (adService.saveAd(ad)) return R.success();
        return R.error();
    }

    @ApiOperation("删除轮播图")
    @DeleteMapping("/delete/{adId}")
    public R deleteAd(@PathVariable("adId") Long adId) {
        if (adService.deleteAd(adId)) return R.success();
        return R.error();
    }

    @ApiOperation("下架广告")
    @PutMapping("/off/{adId}")
    public R offAd(@PathVariable("adId") Long adId) {
        if (adService.offAd(adId)) return R.success();
        return R.error();
    }

    @ApiOperation("上架广告")
    @PutMapping("/on/{adId}")
    public R onAd(@PathVariable("adId") Long adId) {
        if (adService.onAd(adId)) return R.success();
        return R.error();
    }

    @ApiOperation("根据id查询轮播图")
    @GetMapping("/query/{adId}")
    public R queryAdById(@PathVariable("adId") Long adId) {
        return R.success(adService.queryAdById(adId));
    }

    @ApiOperation("搜索轮播图广告")
    @GetMapping("/search")
    public R searchAd(@RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                      @RequestParam(value = "size", required = false, defaultValue = "10") Integer size,
                      @RequestParam(value = "title", required = false, defaultValue = "") String title) {
        Ad ad = new Ad();
        ad.setTitle(title);
        return R.success(adService.query(ad, current, size));
    }

    @ApiOperation("修改广告")
    @PutMapping("/update")
    public R updateAd(@RequestBody Ad ad) {
        if (adService.updateById(ad)) return R.success();
        return R.error();
    }
}

